package com.lkd.handler;

import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutContract;
import com.lkd.contract.VendoutResultContract;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
@Topic(TopicConfig.VMS_RESULT_TOPIC)
public class DealResultOrderHandler implements MsgHandler {
    @Autowired
    OrderService orderService;
    @Override
    public void process(String jsonMsg) throws IOException {
        VendoutResultContract result = JsonUtil.getByJson(jsonMsg, VendoutResultContract.class);
        if(Strings.isNullOrEmpty(result.getInnerCode())) return;
        OrderEntity orderEntity = orderService.getByOrderNo(result.getVendoutData().getOrderNo());
        if(result.isSuccess()){
            orderEntity.setStatus(VMSystem.ORDER_STATUS_VENDOUT_SUCCESS);
            log.info("修改出货状态成功");
        }else {
            orderEntity.setStatus(VMSystem.ORDER_STATUS_VENDOUT_FAIL);
            log.info("出货失败,退款");
            //todo: 发起退款
        }
        orderService.updateById(orderEntity);
    }
}
